<?php

namespace admin\controllers;

use yii\helpers\ArrayHelper;
use common\helpers\Pager;
use common\models\User;
use common\models\Salary;

class SalaryController extends Controller {

    // 设置所属一级导航 不设置表示 就是当前类名
    public $parent = 'member';

    /**
     * @name showing Salary list
     * @return string
     */
    public function actionSalaryList()
    {
        if( ! $this->isJson) {
            return $this->render('salary-list');
        }
        $param = $this->request->post();
        foreach(['start', 'end'] as $key) {
            $param[$key] && $param[$key] = strtotime($param[$key]);
        }
        $param['deleted_at'] = 0;
        $query = Salary::filterConditions(Salary::initCondition(['type', 'user_id', 'employer_id', ['created_at', '>=', 'start'], ['created_at', '<', 'end'], 'status', 'deleted_at'], $param));
        $data['page'] = Pager::page(['page_count' => 20, 'total_count' => $query->count()]);
        $data['infos'] = $query->with('employer')->with('user')->orderBy('id desc')->offset(Pager::offset())->limit(Pager::limit())->asArray()->all();
        return $this->json($data);
    }

    /**
     * @name show salary information
     * @param id int by get request
     * @return string
     */
    public function actionSalaryView()
    {
        $salary = null;
        $salaryId = $this->request->get('id');
        if( ! $salary = Salary::canEdit($salaryId)) {
            return $this->error('无效的薪酬（Invalid Salary）', 'salary/salary-list');
        }
        return $this->render('salary-view', ['data' => $salary]);
    }
}